//
//  CommissionViewController.m
//  DA_V
//
//  Created by 周季阳 on 15/5/5.
//  Copyright (c) 2015年 zhouhai. All rights reserved.
//

#import "CommissionViewController.h"
#import "InterFace.h"
#import "UIViewAdditions.h"
#import "PMCalendar.h"
#import "ProductPriceTableViewCell.h"
#import "YoungAllData.h"
#import "GMDCircleLoader.h"
@interface CommissionViewController ()<PMCalendarControllerDelegate,UITableViewDataSource,UITableViewDelegate,YoungDelegate>
{
    UIView *backView;
    UITableView *zTableView;
}
@property(nonatomic,retain)PMCalendarController *pmCC;
@property (nonatomic, retain) UIButton *dateBtn;

@property(nonatomic, retain)NSString *dateNow;
@property(nonatomic,retain)NSMutableArray *zArrayData;
@end

@implementation CommissionViewController
@synthesize pmCC,dateBtn,dateNow,zArrayData;
#pragma mark - viewDidLoad
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBarHidden=YES;
    
    self.view.backgroundColor = [self colorWithHexString:@"#F5F5F5"];
    [self initNav];
    navTitle.text = @"商品价格";
    
    //获取现在的日期
    [self strtime];
    
    YoungAllData *data = [[YoungAllData alloc]init];
    data.delegate= self;
    [data gainPrice:[[NSUserDefaults standardUserDefaults]objectForKey:@"userID"]];
    [GMDCircleLoader setOnView:self.view withTitle:@"加载中···" animated:YES];
}
#pragma mark - 获取商品价格回调
-(void)price:(NSDictionary *)sign{
    [GMDCircleLoader hideFromView:self.view animated:YES];
    if ([[sign objectForKey:@"status"] isEqualToString:@"success"]) {
    zArrayData = [[NSMutableArray alloc]initWithArray:[sign objectForKey:@"data"]];
    [self initUI];
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:[sign objectForKey:@"reason"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }
}
#pragma mark - 回调失败
-(void)requistFail{

    [GMDCircleLoader hideFromView:self.view animated:YES];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"网络异常,无法加载数据" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
}
#pragma mark - initUI
-(void)initUI{
    //    dateBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect] ;
    //    if (SCREEN_WIDTH == 320) {
    //        dateBtn.frame = CGRectMake(SCREEN_WIDTH-80,29.5, 70,25);
    //    }else if (SCREEN_WIDTH == 375){
    //        dateBtn.frame = CGRectMake(SCREEN_WIDTH-80,35, 70,25);
    //    }else{
    //        dateBtn.frame = CGRectMake(SCREEN_WIDTH-80,48.9, 70,25);
    //    }
    //    dateBtn.layer.borderColor = [UIColor whiteColor].CGColor;
    //    dateBtn.layer.borderWidth = 0.5;
    //    //[self.dateBtn setBackgroundImage:[UIImage imageNamed:@"applySelect"] forState:UIControlStateNormal];
    //    [dateBtn setTintColor:[UIColor whiteColor]];
    //    [dateBtn setTitle:dateNow forState:UIControlStateNormal];
    //    [dateBtn addTarget:self action:@selector(dateBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    //    [self.view  addSubview:dateBtn];
    
    backView = [[UIView alloc]initWithFrame:CGRectMake(0, navView.bottom, SCREEN_WIDTH, 400)];
    backView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:backView];
    
    UIImageView *leftImage = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 19, 21)];
    [leftImage setImage:[UIImage imageNamed:@"商品价格_33"]];
    [backView addSubview:leftImage];
    
    UILabel *topLabel = [[UILabel alloc]initWithFrame:CGRectMake(leftImage.right, 12, SCREEN_WIDTH-leftImage.right-10, 21)];
    topLabel.text = @"本页显示内容为您当前等级所享受的优惠价格";
    topLabel.font = [UIFont systemFontOfSize:14];
    topLabel.textColor = [UIColor blackColor];
    topLabel.textAlignment = NSTextAlignmentLeft;
    [backView addSubview:topLabel];
    
    
    UILabel *zNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, topLabel.bottom+20, SCREEN_WIDTH/2, 30)];
    zNameLabel.textColor = [UIColor grayColor];
    zNameLabel.text = @"商品名称";
    zNameLabel.textAlignment = NSTextAlignmentCenter;
    zNameLabel.font = [UIFont systemFontOfSize:14];
    [backView addSubview:zNameLabel];
    
    
    UILabel *zNumberLabel = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2,topLabel.bottom+20, SCREEN_WIDTH/2-10, 30)];
    zNumberLabel.textColor = [UIColor grayColor];
    zNumberLabel.text = @"商品价格";
    zNumberLabel.textAlignment = NSTextAlignmentCenter;
    zNumberLabel.font = [UIFont systemFontOfSize:14];
    [backView addSubview:zNumberLabel];
    
    [self createrOffLineUI:CGRectMake(10, topLabel.bottom+20, SCREEN_WIDTH-20, 0.5)];
    [self createrOffLineUI:CGRectMake(10, topLabel.bottom+50, SCREEN_WIDTH-20, 0.5)];
    [self createrOffLineUI:CGRectMake(10, topLabel.bottom+20, 0.5, 30)];
    [self createrOffLineUI:CGRectMake(SCREEN_WIDTH-10, topLabel.bottom+20, 0.5, 30)];
    [self createrOffLineUI:CGRectMake(SCREEN_WIDTH/2, topLabel.bottom+20, 0.5, 30)];
    
    
    zTableView = [[UITableView alloc] initWithFrame:CGRectMake(0,topLabel.bottom+51, SCREEN_WIDTH, backView.frame.size.height-60+10)];
    //zTableView.backgroundColor = [UIColor redColor];
    zTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    zTableView.delegate = self;
    zTableView.dataSource = self;
    zTableView.bounces = NO;
    [backView addSubview:zTableView];
    
}
#pragma mark - UitableView delegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return [zArrayData count];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 30;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ProductPriceTableViewCell *cell = [[ProductPriceTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    
    cell.zName.text = [NSString stringWithFormat:@"%@",[[zArrayData objectAtIndex:indexPath.row] objectForKey:@"ProductName"]];
    cell.zNumber.text = [NSString stringWithFormat:@"%@",[[zArrayData objectAtIndex:indexPath.row] objectForKey:@"DiscountPriceAmount"]];
    
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

#pragma mark - 返回
-(void)clickBackBtn{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */
#pragma mark - 获取当前时间
-(void)strtime{
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDate *now;
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |
    NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    now=[NSDate date];
    comps = [calendar components:unitFlags fromDate:now];
    NSInteger year = [comps year];
    NSInteger month = [comps month];
    NSInteger day = [comps day];
    NSString *monthStr ;
    NSString *dayStr ;
    if (month < 10 || day < 10) {
        if (month <10) {
            monthStr = [NSString stringWithFormat:@"0%ld",month];
        }else{
            monthStr = [NSString stringWithFormat:@"%ld",month];
        }
        if(day < 10){
            dayStr = [NSString stringWithFormat:@"0%ld",day];
        }else{
            dayStr = [NSString stringWithFormat:@"%ld",day];
        }
    }else{
        monthStr = [NSString stringWithFormat:@"%ld",month];
        dayStr = [NSString stringWithFormat:@"%ld",day];
    }
    
    //dateNow = [NSString stringWithFormat:@"%ld-%@-%@",(long)year,monthStr,dayStr];
    dateNow = [NSString stringWithFormat:@"%ld-%@",(long)year,monthStr];
}
-(void)createrOffLineUI:(CGRect)rect{
    UIView *lines = [[UIView alloc] initWithFrame:rect];
    lines.backgroundColor = [UIColor colorWithRed:179/255.0 green:179/255.0 blue:179/255.0 alpha:1];
    [backView addSubview:lines];
}

@end
